<cfcomponent>
	<cffunction name="update" access="public" returntype="string">
		<cfargument name="username" type="string" required="yes">
		<cfargument name="password" type="string" required="yes">
		<cfargument name="status" type="string" required="yes">
		
			<cfhttp url="http://twitter.com/statuses/update.xml" username="#arguments.username#" password="#arguments.password#" method="post" charset="utf-8" >
			<cfhttpparam type="formfield" name="status" value="#Mid(arguments.status,1,160)#" />
		</cfhttp>
		
		<cfreturn cfhttp.Statuscode>
	</cffunction>
	
	<cffunction name="getReplies" access="public" returntype="string">
		<cfargument name="username" type="string" required="yes">
		<cfargument name="password" type="string" required="yes">
		
		<cfhttp url=" http://twitter.com/statuses/replies.xml" username="#arguments.username#" password="#arguments.password#" method="get" charset="utf-8" >
		</cfhttp>
		
		<cfreturn cfhttp.FileContent>
	</cffunction>

	<cffunction name="updateFromReplies" access="public" returntype="void">
		<cfargument name="username" type="string" required="yes">
		<cfargument name="password" type="string" required="yes">
		<cfargument name="fileName" type="string" required="yes">

		<cfset var previousRepliesXML = "">
		<cfset var previousReplies = "">
		<cfset var replies = "">
		<cfset var repliesXML = "">
		<cfset var previousIDs = ArrayNew(1)>
		<cfset var status = "">
		<cfset var newFile = "">
		<cfset var dummy = "">

		<cftry>
		<cffile action="read" charset="utf-8" file="#arguments.fileName#" variable="previousReplies">
		<cfset newFile = False>
		<cfcatch type="any">
			<cfset newFile = True>
		</cfcatch>
		</cftry>
		
		<cfscript>
			if (not newFile) {
				previousRepliesXML = XmlParse(previousReplies);
			}
			replies = getReplies(arguments.username, arguments.password);
			repliesXML = XmlParse(replies);
		</cfscript>
		
		<cfloop index="i" from="1" to="#ArrayLen(repliesXML.statuses.XmlChildren)#">
			<cfif not newFile>
				<cfset previousIDs = XmlSearch(previousRepliesXML, "/statuses/status[id=#repliesXML.statuses.status[i].id.XmlText#]")>
			</cfif>
			<cfif arrayLen(previousIDs) eq 0>
				<cfset status = ReplaceNoCase (repliesXML.statuses.status[i].text.XmlText, "@#username#", "#repliesXML.statuses.status[i].user.screen_name.XmlText#:")>
				<cfset dummy = update(arguments.username, arguments.password, status)>
			</cfif>
		</cfloop>
		
		<cffile action="write" file="#arguments.fileName#" output="#replies#">
		
	</cffunction>		

</cfcomponent>